package com.ricchieri.manage.bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.OperationFilter;
import com.ricchieri.filter.OperationTotalAmountFilter;
import com.ricchieri.filter.UserFilter;
import com.ricchieri.lazymodel.UserAccountStateLazyDataModel;
import com.ricchieri.model.Operation;
import com.ricchieri.model.OperationTotalAmount;
import com.ricchieri.model.User;
import com.ricchieri.service.interf.IOperationService;
import com.ricchieri.service.interf.IOperationTotalAmountService;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "viewUserAccountStateMB")
@ViewScoped
public class ViewUserAccountStateManageBean extends CommonOperationManageBean {
	private static final long serialVersionUID = 2740167755239733978L;

	private User user;
	private UserAccountStateLazyDataModel userAccountStateLazyDataModel; 
	private OperationFilter operationFilter = new OperationFilter(); 
	private UserFilter userFilter;
	
	@ManagedProperty(value="#{OperationService}")
	IOperationService operationService;

	@ManagedProperty(value="#{OperationTotalAmountService}")
	IOperationTotalAmountService operationTotalAmountService;
	
	public void init(){
		user = (User) FacesUtil.getParametersFlash(BusinessSettings.KEY_USER);
		userFilter = (UserFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
		
		if(!FacesContext.getCurrentInstance().isPostback() &&  user == null) {
			FacesUtil.redirectToNavigationCase(BusinessSettings.SEARCH_OPERATION);
		}
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public String addOperation(){
		FacesUtil.addParametersFlash(BusinessSettings.KEY_USER, user);
		
		return BusinessSettings.ADD;
	}

	public UserAccountStateLazyDataModel getUserAccountStateLazyDataModel() {
		if(userAccountStateLazyDataModel == null && getUser() != null){
			operationFilter.setSortField("id");
			operationFilter.setSortOrder(BusinessSettings.SORT_DESCENDING);
			operationFilter.setUserId(getUser().getId());
			userAccountStateLazyDataModel = new UserAccountStateLazyDataModel(operationService, operationFilter);
		}
		
		return userAccountStateLazyDataModel;
	}

	public void setUserAccountStateLazyDataModel(
			UserAccountStateLazyDataModel userAccountStateLazyDataModel) {
		this.userAccountStateLazyDataModel = userAccountStateLazyDataModel;
	}

	public OperationFilter getOperationFilter() {
		return operationFilter;
	}

	public void setOperationFilter(OperationFilter operationFilter) {
		this.operationFilter = operationFilter;
	}

	public IOperationService getOperationService() {
		return operationService;
	}

	public void setOperationService(IOperationService operationService) {
		this.operationService = operationService;
	}
	
	public OperationTotalAmount getOperationTotalAmount(Operation operation) {
		OperationTotalAmountFilter filter = new OperationTotalAmountFilter();
		filter.setOperation(operation.getId());
		return operationTotalAmountService.getOperationTotalAmountById(filter);
	}

	public IOperationTotalAmountService getOperationTotalAmountService() {
		return operationTotalAmountService;
	}

	public void setOperationTotalAmountService(
			IOperationTotalAmountService operationTotalAmountService) {
		this.operationTotalAmountService = operationTotalAmountService;
	}
	
	public String viewDetails(Operation operation) {
		
		FacesUtil.addParametersFlash(BusinessSettings.KEY_OPEARATION, operation);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, userFilter);
		return BusinessSettings.VIEW_DETAILS;
	}
	
	public String doBack() {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, userFilter);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_USER, user);
		
		return BusinessSettings.BACK;
	}
	
}

